package com.xjx.controller;

import com.xjx.service.SMSService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
@RestController
@RequestMapping("/api/sms")
public class SMSController {

    private static final Logger logger = LoggerFactory.getLogger(SMSController.class);

    private final SMSService smsService;

    public SMSController(SMSService smsService) {
        this.smsService = smsService;
    }

    /**
     * 发送测试短信
     * <p>
     * 接收JSON格式的请求体，仅需提供手机号码。
     * </p>
     *
     * @param payload 包含手机号的JSON对象。
     * <br>
     * <b>请求示例:</b>
     * <pre>
     * {@code
     * {
     * "to": "135xxxxxxxx"
     * }
     * }
     * </pre>
     * @return 一个 ResponseEntity 对象，其中包含操作成功或失败的信息。
     */
    @PostMapping("/send")
    public ResponseEntity<String> sendTestSms(@RequestBody Map<String, String> payload) {
        String to = payload.get("to");
        if (to == null || to.trim().isEmpty()) {
            return ResponseEntity.badRequest().body("请求失败：参数 'to' 不能为空。");
        }

        try {
            boolean success = smsService.sendTestSMS(to);
            if (success) {
                return ResponseEntity.ok("测试短信发送请求已成功处理。");
            } else {
                return ResponseEntity.status(500).body("测试短信发送请求处理失败，请检查服务器日志。");
            }
        } catch (Exception e) {
            logger.error("处理测试短信发送请求时发生未知异常", e);
            return ResponseEntity.status(500).body("服务器内部错误：" + e.getMessage());
        }
    }
}

